package com.czkt.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.czkt.pojo.Product;
import com.czkt.service.IProductService;
import com.czkt.utils.PageUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/product")
public class ProductController {

    @Autowired
    IProductService productService;

    @RequestMapping("/list")
    public Object list(Product product, @RequestParam(defaultValue = "1") Integer pageIndex,@RequestParam(defaultValue = "2") Integer pageSize, Model model){
        IPage<Product> page = new Page<>(pageIndex,pageSize);

        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().like(StringUtils.hasText(product.getProdName()),Product::getProdName,product.getProdName())
                .like(StringUtils.hasText(product.getProdType()),Product::getProdType,product.getProdType())
                .like(StringUtils.hasText(product.getProdBatch()),Product::getProdBatch,product.getProdBatch());
        page = productService.page(page,queryWrapper);

        PageUtils<Product> pageUtils = new PageUtils<>();

        BeanUtils.copyProperties(page,pageUtils);

        model.addAttribute("productPage",pageUtils);
        model.addAttribute("prodName",product.getProdName());
        model.addAttribute("prodType",product.getProdType());
        model.addAttribute("prodBatch",product.getProdBatch());
        return "product/list";
    }
}
